package net.homeip.jcperera.webinfo.beans;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import net.homeip.jcperera.nu.core.boot.Boot;
import net.homeip.jcperera.nu.core.msg.BaseMessage;
import net.homeip.jcperera.nu.core.msg.MessageBagRequest;
import net.homeip.jcperera.webinfo.ScanProfile;
import net.homeip.jcperera.webinfo.ScanProfileParameter;

import org.primefaces.event.SelectEvent;

@ManagedBean
@ViewScoped
public class ScanProfileParameterBean implements Serializable
{
    private static final long    serialVersionUID = 5868621129231959726L;

    private ScanProfileParameter scanProfileParameter;

    public ScanProfileParameterBean()
    {
        scanProfileParameter = new ScanProfileParameter();
    }

    public List<ScanProfileParameter> getScanProfileParameters()
    {
        List<ScanProfileParameter> scanProfileParameters = new ArrayList<ScanProfileParameter>();

        if (scanProfileParameter != null && scanProfileParameter.getScanProfile() != null)
        {
            scanProfileParameters.addAll(scanProfileParameter.getScanProfile().getScanProfileParameters());
        }
        return scanProfileParameters;
    }

    public List<ScanProfileParameter> getAllScanProfileParameters()
    {
        List<ScanProfileParameter> scanProfiles = new ArrayList<ScanProfileParameter>();
        EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
        EntityManager entityManager = (EntityManager) emf.createEntityManager();
        entityManager.getTransaction().begin();
        TypedQuery<ScanProfileParameter> sppQuery = entityManager.createQuery(
                "SELECT OBJECT(spp) FROM ScanProfileParameter spp", ScanProfileParameter.class);
        scanProfiles = sppQuery.getResultList();
        entityManager.getTransaction().commit();
        entityManager.close();
        return scanProfiles;
    }

    public void saveScanProfileParameter(ActionEvent actionEvent)
    {
        saveScanProfileParameter();
    }

    public String saveScanProfileParameter()
    {
        if (scanProfileParameter != null)
        {
            EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
            EntityManager entityManager = (EntityManager) emf.createEntityManager();
            entityManager.getTransaction().begin();
            if (scanProfileParameter.getEntityid() != null && scanProfileParameter.getEntityid().length() > 0)
                scanProfileParameter = entityManager.merge(scanProfileParameter);
            entityManager.persist(scanProfileParameter);
            entityManager.getTransaction().commit();
            entityManager.getEntityManagerFactory().getCache().evictAll();
            entityManager.close();
            MessageBagRequest messageBagRequest = new MessageBagRequest();
            messageBagRequest.addInformationalMessage("nu.core.entity.saved.ok", BaseMessage.FRIENDLINESS_FRIENDLY);
            messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
        }
        return "edit.xhtml?faces-redirect=true&entityid=" + this.getEntityid();
    }

    public void deleteScanProfile(ActionEvent actionEvent)
    {
        deleteScanProfileParameter();
    }

    public String deleteScanProfileParameter()
    {
        if (scanProfileParameter != null)
        {
            MessageBagRequest messageBagRequest = new MessageBagRequest();
            String entityid = scanProfileParameter.getEntityid();
            if (entityid != null && entityid.trim().length() > 0)
            {
                EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
                EntityManager entityManager = (EntityManager) emf.createEntityManager();
                entityManager.getTransaction().begin();
                scanProfileParameter = entityManager.merge(scanProfileParameter);
                entityManager.remove(scanProfileParameter);
                messageBagRequest.addInformationalMessage("nu.core.entity.deleted.ok",
                        BaseMessage.FRIENDLINESS_FRIENDLY);
                entityManager.getTransaction().commit();
                entityManager.getEntityManagerFactory().getCache().evictAll();
                entityManager.close();
            }
            messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
        }
        return "/pages/scanprofile/edit.xhtml?faces-redirect=true&entityid=" + this.getParentid();
    }

    public String getEntityid()
    {
        return scanProfileParameter.getEntityid();
    }

    public void setEntityid(String entityid)
    {
        MessageBagRequest messageBagRequest = new MessageBagRequest();
        if (entityid != null)
        {
            EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
            EntityManager entityManager = (EntityManager) emf.createEntityManager();
            entityManager.getTransaction().begin();
            ScanProfileParameter scanProfileParameter = null;
            TypedQuery<ScanProfileParameter> wisQuery = entityManager.createQuery(
                    "SELECT OBJECT(spp) FROM ScanProfileParameter spp WHERE spp.entityid = :entityid",
                    ScanProfileParameter.class);
            wisQuery.setParameter("entityid", entityid);
            scanProfileParameter = wisQuery.getSingleResult();
            if (scanProfileParameter == null)
            {
                Hashtable<String, String> extensions = new Hashtable<String, String>();
                extensions.put("id", entityid);
                extensions.put("description", "Entity not found on database");
                messageBagRequest.addErrorMessage("nu.core.entity.type.load.error", BaseMessage.FRIENDLINESS_FRIENDLY,
                        extensions);
            }
            else this.scanProfileParameter = scanProfileParameter;
            entityManager.getTransaction().commit();
            entityManager.close();
        }
        messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
    }

    public String getParentid()
    {
        return ((scanProfileParameter != null && scanProfileParameter.getScanProfile() != null) ? scanProfileParameter
                .getScanProfile().getEntityid() : null);
    }

    public void setParentid(String entityid)
    {
        MessageBagRequest messageBagRequest = new MessageBagRequest();
        if (entityid != null)
        {
            EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
            EntityManager entityManager = (EntityManager) emf.createEntityManager();
            entityManager.getTransaction().begin();
            ScanProfile scanProfile = null;
            TypedQuery<ScanProfile> scpQuery = entityManager.createQuery(
                    "SELECT OBJECT(scp) FROM ScanProfile scp WHERE scp.entityid = :entityid", ScanProfile.class);
            scpQuery.setParameter("entityid", entityid);
            scanProfile = scpQuery.getSingleResult();
            if (scanProfile == null)
            {
                Hashtable<String, String> extensions = new Hashtable<String, String>();
                extensions.put("id", entityid);
                extensions.put("description", "Entity not found on database");
                messageBagRequest.addErrorMessage("nu.core.entity.type.load.error", BaseMessage.FRIENDLINESS_FRIENDLY,
                        extensions);
            }
            else this.scanProfileParameter.setScanProfile(scanProfile);
            entityManager.getTransaction().commit();
            entityManager.close();
        }
        messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
    }

    public void onRowSelect(SelectEvent event)
    {
        try
        {
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("../scanprofileparameter/edit.xhtml?entityid=" + scanProfileParameter.getEntityid());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getName()
    {
        return scanProfileParameter.getName();
    }

    public void setName(String name)
    {
        scanProfileParameter.setName(name);
    }

    public String getCode()
    {
        return scanProfileParameter.getCode();
    }

    public void setCode(String code)
    {
        scanProfileParameter.setCode(code);
    }

    public String getFixedValue()
    {
        return scanProfileParameter.getFixedValue();
    }

    public void setFixedValue(String fixedValue)
    {
        scanProfileParameter.setFixedValue(fixedValue);
    }
    
    public String getCurrentValue()
    {
        return scanProfileParameter.getCurrentValue();
    }

    public void setCurrentValue(String currentValue)
    {
        scanProfileParameter.setCurrentValue(currentValue);
    }
    
    public ScanProfile getScanProfile()
    {
        return scanProfileParameter.getScanProfile();
    }

    public void setScanProfile(ScanProfile scanProfile)
    {
        scanProfileParameter.setScanProfile(scanProfile);
    }
    
    public ScanProfileParameter getSelectedScanProfileParameter()
    {
        return this.scanProfileParameter;
    }
    
    public void setSelectedScanProfileParameter(ScanProfileParameter scanProfileParameter)
    {
        this.scanProfileParameter = scanProfileParameter;
    }
}
